package org.zfes.snowier.cloud.zbss.layer.tc.biz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.idcenter.IDGenerator;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.IOrgnizationService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.dao.OrgnizationMapper;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.Orgnization;

@Service
public class OrgnizationServiceImpl implements IOrgnizationService{
 	@Autowired
	private OrgnizationMapper orgnizationMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createOrgnization(Orgnization orgnization) {
		ZBeanUtil.validateBean(orgnization);
		
		Object id1=orgnizationMapper.selectIdByName(orgnization.getName());
		if(id1!=null){
			ZAlert.Error("单位名称已经存在");
		}
		orgnization.setId(IDGenerator.genLongId());
		orgnizationMapper.insertSelective(orgnization);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Orgnization> loadOrgnizationById(Long id) {
		if(id!=null){
			return Optional.ofNullable(orgnizationMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateOrgnization(Orgnization orgnization) {
		ZBeanUtil.validateBean(orgnization);
		Object id1=orgnizationMapper.selectIdByName(orgnization.getName());
		if(id1!=null&&id1.equals(orgnization.getId())){
			ZAlert.Error("单位名称已经存在");
		}
		orgnizationMapper.updateByPrimaryKeySelective(orgnization);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletOrgnization(Long id) {
		if(id!=null){
			orgnizationMapper.deleteByPrimaryKey(id);
			return;
		}
		ZAlert.Error("请选择要删除的数据");
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadOrgnizationDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS(orgnizationMapper.selectListCount(pm), orgnizationMapper.selectList(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Orgnization> loadByName(java.lang.String name){
		if(name!=null){
			return Optional.ofNullable(orgnizationMapper.selectByName(name));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	@Override
	public void setEnabled(List<Long> ids, Boolean enabled) {
		ZAssert.notNull(ids, "请选择数据");
		ZAssert.notNull(enabled, "状态参数不能为空");
		int value=enabled?1:0;
		orgnizationMapper.updateEnabled( ids,  value);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadComboboVo(Boolean firstEmpty) {
		List<ComboboVo> appList=orgnizationMapper.selectComboboVO();
		 List<ComboboVo> appList2=new ArrayList<>();
		 if(firstEmpty!=null&&firstEmpty){
			 appList2.add(0, ComboboVo.newEmptyFirst()); 
		 }
		 appList2.addAll(appList);
		return appList2;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	@Override
	public void setOrgnizationEnabled(List<Long> ids, Boolean enabled) {
		ZAssert.notNull(ids, "请选择数据");
		ZAssert.notNull(enabled, "状态参数不能为空");
		int value=enabled?1:0;
		orgnizationMapper.updateEnabled( ids,  value);
	}
}